package com.js.lib_common.http;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wz on 2019/5/17.
 */
public class ApiClient {
    public static Retrofit mRetrofit;

    public static Retrofit retrofit(String baseUr) {
        if (mRetrofit == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
//            if (BuildConfig.DEBUG) {
                // Log信息拦截器
                HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                //设置 Debug Log 模式
                builder.addInterceptor(loggingInterceptor);
//            }

            //设置超时
            builder.connectTimeout(15, TimeUnit.SECONDS);
            builder.readTimeout(15, TimeUnit.SECONDS);
            builder.writeTimeout(15, TimeUnit.SECONDS);
           
            //错误重连
            builder.retryOnConnectionFailure(true);
//            builder.addInterceptor(new BasePresenter.HeaderInterceptor());//添加共同header
//            builder.addInterceptor(new TokenInterceptor());//添加获取token的拦截器
            //缓存
//            File cacheFile = new File(FlapDoorApplication.getAppContext().getExternalCacheDir(), "GasCache");
//            Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
//            builder.addNetworkInterceptor(new NetCacheInterceptor())
//                    .addInterceptor(new OfflineCacheInterceptor())
//                    .cache(cache);
            //cookie
//            builder.cookieJar(new CookieManger(FlapDoorApplication.getAppContext()));
            OkHttpClient okHttpClient = builder.build();
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(baseUr)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }

        return mRetrofit;
    }

}
